/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Codigo;

import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author kyerte
 */
public class FrmReportes extends javax.swing.JFrame {

    public static Connection conexion;

    /**
     * Creates new form FrmReportes
     */
    public FrmReportes() {
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagenes/Reportes.png")).getImage());
        this.getContentPane().setBackground(new java.awt.Color(255, 255, 255));
        initComponents();
        conexion = FrmPrincipal.conexion;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmdCatalogoClientes = new javax.swing.JButton();
        cmdVisitasClientes = new javax.swing.JButton();
        cmdPropiedades = new javax.swing.JButton();
        cmdVisitas = new javax.swing.JButton();
        cmdMedios = new javax.swing.JButton();
        cmdLlamadas = new javax.swing.JButton();
        cmdPropiedades1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Reportes");
        setResizable(false);

        cmdCatalogoClientes.setBackground(java.awt.Color.white);
        cmdCatalogoClientes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Clients.png"))); // NOI18N
        cmdCatalogoClientes.setText("Catálogo clientes");
        cmdCatalogoClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCatalogoClientesActionPerformed(evt);
            }
        });

        cmdVisitasClientes.setBackground(java.awt.Color.white);
        cmdVisitasClientes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Casa.png"))); // NOI18N
        cmdVisitasClientes.setText("Visitas clientes");
        cmdVisitasClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdVisitasClientesActionPerformed(evt);
            }
        });

        cmdPropiedades.setBackground(java.awt.Color.white);
        cmdPropiedades.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Tipos.png"))); // NOI18N
        cmdPropiedades.setText("Catálogo propiedades");
        cmdPropiedades.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdPropiedadesActionPerformed(evt);
            }
        });

        cmdVisitas.setBackground(java.awt.Color.white);
        cmdVisitas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Casas.png"))); // NOI18N
        cmdVisitas.setText("Reporte visitas");
        cmdVisitas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdVisitasActionPerformed(evt);
            }
        });

        cmdMedios.setBackground(java.awt.Color.white);
        cmdMedios.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/wizard35.png"))); // NOI18N
        cmdMedios.setText("Estadísticas");
        cmdMedios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdMediosActionPerformed(evt);
            }
        });

        cmdLlamadas.setBackground(java.awt.Color.white);
        cmdLlamadas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Phone-32.png"))); // NOI18N
        cmdLlamadas.setText("Llamadas");
        cmdLlamadas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdLlamadasActionPerformed(evt);
            }
        });

        cmdPropiedades1.setBackground(java.awt.Color.white);
        cmdPropiedades1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Favourites-32.png"))); // NOI18N
        cmdPropiedades1.setText("Reporte interéses");
        cmdPropiedades1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdPropiedades1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(cmdCatalogoClientes, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(cmdPropiedades, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cmdMedios, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(cmdLlamadas, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cmdVisitas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cmdVisitasClientes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addComponent(cmdPropiedades1, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmdCatalogoClientes, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                    .addComponent(cmdPropiedades, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdMedios, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdVisitasClientes, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdLlamadas, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdVisitas, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addComponent(cmdPropiedades1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdVisitasClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdVisitasClientesActionPerformed
        JComboBox cboClientes = new JComboBox(obtenerClientes());

        Object[] controles = {"Cliente", cboClientes};

        int sel = JOptionPane.showConfirmDialog(null, controles,
                "Generar reporte", JOptionPane.OK_CANCEL_OPTION);

        if (sel == JOptionPane.OK_OPTION) {

            String nombreCliente = cboClientes.getSelectedItem().toString();
            Map param = new HashMap();
            String template = "";
            JasperReport reporte = null;
            param.put("NombreCliente", nombreCliente);
            JasperPrint jasperprint;
            template = "VisitaClientes.jasper";

            try {

                Class.forName("com.mysql.jdbc.Driver");
                reporte = (JasperReport) JRLoader.loadObject(template);
                jasperprint = JasperFillManager.fillReport(reporte, param, conexion);
                JasperViewer visorReporte = new JasperViewer(jasperprint, false);
                visorReporte.setTitle("Visita de cliente");
                visorReporte.setVisible(true);

            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            } catch (JRException ex) {
                ex.printStackTrace();
            }

        }
    }//GEN-LAST:event_cmdVisitasClientesActionPerformed
    private Vector obtenerClientes() {

        Vector nombresClientes = new Vector();
        ResultSet conjuntoResultados;

        try {

            Statement obtenerClientes = conexion.createStatement();
            conjuntoResultados = obtenerClientes.executeQuery("SELECT Nombre FROM Clientes");

            while (conjuntoResultados.next()) {
                nombresClientes.addElement(conjuntoResultados.getString(1));
            }

        } catch (SQLException ex) {
            ex.printStackTrace();

        }

        return nombresClientes;
    }
    private void cmdCatalogoClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCatalogoClientesActionPerformed
        catalogoClientes();
    }//GEN-LAST:event_cmdCatalogoClientesActionPerformed

    private void cmdPropiedadesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPropiedadesActionPerformed

        JRadioButton optCorretaje = new JRadioButton("Corretaje", true);
        JRadioButton optDesarrollo = new JRadioButton("Desarrollo");
        ButtonGroup grupo = new ButtonGroup();

        grupo.add(optCorretaje);
        grupo.add(optDesarrollo);

        Object[] controles = {optCorretaje, optDesarrollo};

        int sel = JOptionPane.showConfirmDialog(null, controles,
                "Generar reporte", JOptionPane.OK_CANCEL_OPTION);

        if (sel == JOptionPane.OK_OPTION) {
            if (optCorretaje.isSelected()) {
                catalogoCorretaje();
            } else if (optDesarrollo.isSelected()) {
                catalogoDesarrollos();
            }
        }
    }//GEN-LAST:event_cmdPropiedadesActionPerformed

    private void cmdVisitasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdVisitasActionPerformed
        JRadioButton optFecha = new JRadioButton("Fecha", true);
        com.toedter.calendar.JDateChooser fechaInicial =
                new com.toedter.calendar.JDateChooser();
        com.toedter.calendar.JDateChooser fechaFinal =
                new com.toedter.calendar.JDateChooser();
        JRadioButton optCliente = new JRadioButton("Cliente");
        JComboBox cboClientes = new JComboBox(obtenerClientes());
        ButtonGroup grupo = new ButtonGroup();

        grupo.add(optFecha);
        grupo.add(optCliente);

        Object[] controles = {optFecha, fechaInicial, fechaFinal, optCliente, cboClientes};

        int sel = JOptionPane.showConfirmDialog(null, controles,
                "Generar reporte", JOptionPane.OK_CANCEL_OPTION);

        if (sel == JOptionPane.OK_OPTION) {
            if (optFecha.isSelected()) {
                JasperReport reporte = null;
                JasperPrint jasperprint;
                Map param = new HashMap();

                try {
                    long fechaInicio = fechaInicial.getDate().getTime();
                    java.sql.Date fechaInicioSQL = new java.sql.Date(fechaInicio);
                    long fechaFin = fechaFinal.getDate().getTime();
                    java.sql.Date fechaFinSQL = new java.sql.Date(fechaFin);
                    param.put("fechaInicial", fechaInicioSQL);
                    param.put("fechaFinal", fechaFinSQL);

                } catch (NullPointerException ex) {
                    JOptionPane.showMessageDialog(null, "Elige fechas",
                            "Generar reporte", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                try {
                    reporte = (JasperReport) JRLoader.loadObject("Reporte2.jasper");
                    jasperprint = JasperFillManager.fillReport(reporte, param, conexion);
                    JasperViewer visorReporte = new JasperViewer(jasperprint, false);
                    visorReporte.setTitle("Reporte visitas");
                    visorReporte.setVisible(true);
                } catch (JRException ex) {
                    ex.printStackTrace();
                }
            } else if (optCliente.isSelected()) {
                String nombreCliente = cboClientes.getSelectedItem().toString();
                Map param = new HashMap();
                String template = "";
                JasperReport reporte = null;
                param.put("nombreCliente", nombreCliente);
                JasperPrint jasperprint;
                template = "Reporte2_Nombre.jasper";

                try {

                    Class.forName("com.mysql.jdbc.Driver");
                    reporte = (JasperReport) JRLoader.loadObject(template);
                    jasperprint = JasperFillManager.fillReport(reporte, param, conexion);
                    JasperViewer visorReporte = new JasperViewer(jasperprint, false);
                    visorReporte.setTitle("Reporte visitas");
                    visorReporte.setVisible(true);

                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (JRException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }//GEN-LAST:event_cmdVisitasActionPerformed

    private void cmdMediosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdMediosActionPerformed
        graficosVisitas();
    }//GEN-LAST:event_cmdMediosActionPerformed

    private void cmdLlamadasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdLlamadasActionPerformed
        reporteLlamadas();
    }//GEN-LAST:event_cmdLlamadasActionPerformed

    private void cmdPropiedades1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPropiedades1ActionPerformed
        reporteIntereses();
    }//GEN-LAST:event_cmdPropiedades1ActionPerformed

    private void catalogoClientes() {
        JasperReport reporte = null;
        JasperPrint jasperprint;
        Map param = new HashMap();

        try {
            reporte = (JasperReport) JRLoader.loadObject("CatalogoClientes.jasper");
            jasperprint = JasperFillManager.fillReport(reporte, param, conexion);
            JasperViewer visorReporte = new JasperViewer(jasperprint, false);
            visorReporte.setTitle("Catálogo de clientes");
            visorReporte.setVisible(true);
        } catch (JRException ex) {
            ex.printStackTrace();
        }
    }

    private void catalogoCorretaje() {
        JasperReport reporte = null;
        JasperPrint jasperprint;
        Map param = new HashMap();

        try {
            reporte = (JasperReport) JRLoader.loadObject("CatalogoCorretaje.jasper");
            jasperprint = JasperFillManager.fillReport(reporte, param, conexion);
            JasperViewer visorReporte = new JasperViewer(jasperprint, false);
            visorReporte.setTitle("Corretaje");
            visorReporte.setVisible(true);
        } catch (JRException ex) {
            ex.printStackTrace();
        }
    }

    private void catalogoDesarrollos() {
        JasperReport reporte = null;
        JasperPrint jasperprint;
        Map param = new HashMap();

        try {
            reporte = (JasperReport) JRLoader.loadObject("CatalogoDesarrollos.jasper");
            jasperprint = JasperFillManager.fillReport(reporte, param, conexion);
            JasperViewer visorReporte = new JasperViewer(jasperprint, false);
            visorReporte.setTitle("Corretaje");
            visorReporte.setVisible(true);
        } catch (JRException ex) {
            ex.printStackTrace();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdCatalogoClientes;
    private javax.swing.JButton cmdLlamadas;
    private javax.swing.JButton cmdMedios;
    private javax.swing.JButton cmdPropiedades;
    private javax.swing.JButton cmdPropiedades1;
    private javax.swing.JButton cmdVisitas;
    private javax.swing.JButton cmdVisitasClientes;
    // End of variables declaration//GEN-END:variables

    private void graficosVisitas() {

        com.toedter.calendar.JDateChooser fechaInicial =
                new com.toedter.calendar.JDateChooser();
        com.toedter.calendar.JDateChooser fechaFinal =
                new com.toedter.calendar.JDateChooser();
        Object[] controles = {"Fecha inicial", fechaInicial, "Fecha final", fechaFinal};

        int sel = JOptionPane.showConfirmDialog(null, controles,
                "Generar reporte", JOptionPane.OK_CANCEL_OPTION);

        if (sel == JOptionPane.OK_OPTION) {

            JasperReport reporte = null;
            JasperPrint jasperprint;
            Map param = new HashMap();

            try {
                long fechaInicio = fechaInicial.getDate().getTime();
                java.sql.Date fechaInicioSQL = new java.sql.Date(fechaInicio);
                long fechaFin = fechaFinal.getDate().getTime();
                java.sql.Date fechaFinSQL = new java.sql.Date(fechaFin);
                param.put("fechaInicial", fechaInicioSQL);
                param.put("fechaFinal", fechaFinSQL);

            } catch (NullPointerException ex) {
                JOptionPane.showMessageDialog(null, "Elige fechas",
                        "Generar reporte", JOptionPane.WARNING_MESSAGE);
                return;
            }

            try {
                reporte = (JasperReport) JRLoader.loadObject("GraficosVisitas.jasper");
                jasperprint = JasperFillManager.fillReport(reporte, param, conexion);
                JasperViewer visorReporte = new JasperViewer(jasperprint, false);
                visorReporte.setTitle("Gráficos visitas");
                visorReporte.setVisible(true);
            } catch (JRException ex) {
                ex.printStackTrace();
            }
        }
    }

    private void reporteLlamadas() {
        com.toedter.calendar.JDateChooser fechaInicial =
                new com.toedter.calendar.JDateChooser();
        com.toedter.calendar.JDateChooser fechaFinal =
                new com.toedter.calendar.JDateChooser();
        Object[] controles = {"Fecha inicial", fechaInicial, "Fecha final", fechaFinal};

        int sel = JOptionPane.showConfirmDialog(null, controles,
                "Generar reporte", JOptionPane.OK_CANCEL_OPTION);

        if (sel == JOptionPane.OK_OPTION) {

            JasperReport reporte = null;
            JasperPrint jasperprint;
            Map param = new HashMap();

            try {
                long fechaInicio = fechaInicial.getDate().getTime();
                java.sql.Date fechaInicioSQL = new java.sql.Date(fechaInicio);
                long fechaFin = fechaFinal.getDate().getTime();
                java.sql.Date fechaFinSQL = new java.sql.Date(fechaFin);
                param.put("fechaInicial", fechaInicioSQL);
                param.put("fechaFinal", fechaFinSQL);

            } catch (NullPointerException ex) {
                JOptionPane.showMessageDialog(null, "Elige fechas",
                        "Generar reporte", JOptionPane.WARNING_MESSAGE);
                return;
            }

            try {
                reporte = (JasperReport) JRLoader.loadObject("Llamadas.jasper");
                jasperprint = JasperFillManager.fillReport(reporte, param, conexion);
                JasperViewer visorReporte = new JasperViewer(jasperprint, false);
                visorReporte.setTitle("Reporte llamadas");
                visorReporte.setVisible(true);
            } catch (JRException ex) {
                ex.printStackTrace();
            }
        }
    }

    private void reporteIntereses() {
        com.toedter.calendar.JDateChooser fechaInicial =
                new com.toedter.calendar.JDateChooser();
        com.toedter.calendar.JDateChooser fechaFinal =
                new com.toedter.calendar.JDateChooser();
        Object[] controles = {"Fecha inicial", fechaInicial, "Fecha final", fechaFinal};

        int sel = JOptionPane.showConfirmDialog(null, controles,
                "Generar reporte", JOptionPane.OK_CANCEL_OPTION);

        if (sel == JOptionPane.OK_OPTION) {

            JasperReport reporte = null;
            JasperPrint jasperprint;
            Map param = new HashMap();

            try {
                long fechaInicio = fechaInicial.getDate().getTime();
                java.sql.Date fechaInicioSQL = new java.sql.Date(fechaInicio);
                long fechaFin = fechaFinal.getDate().getTime();
                java.sql.Date fechaFinSQL = new java.sql.Date(fechaFin);
                param.put("fechaInicial", fechaInicioSQL);
                param.put("fechaFinal", fechaFinSQL);

            } catch (NullPointerException ex) {
                JOptionPane.showMessageDialog(null, "Elige fechas",
                        "Generar reporte", JOptionPane.WARNING_MESSAGE);
                return;
            }

            try {
                reporte = (JasperReport) JRLoader.loadObject("ReporteIntereses.jasper");
                jasperprint = JasperFillManager.fillReport(reporte, param, conexion);
                JasperViewer visorReporte = new JasperViewer(jasperprint, false);
                visorReporte.setTitle("Reporte interéses");
                visorReporte.setVisible(true);
            } catch (JRException ex) {
                ex.printStackTrace();
            }
        }
    }
}
